<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title> 
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main"> 
		<!-- 模糊搜索 -->
		 <fieldset class="table-search-fieldset">
			<legend>模糊搜索</legend>
			<div style="margin: 10px 10px 10px 10px">
				<form class="layui-form layui-form-pane" action="">
					<div class="layui-form-item">
						<div class="layui-inline">
							<label class="layui-form-label">产品ID</label>
							<div class="layui-input-inline">
								<input type="number" id="pid" name="pid"  autocomplete="off" class="layui-input">
							</div>
						</div>
						<div class="layui-inline">
							<label class="layui-form-label">产品品类</label>
							<div class="layui-input-inline">
								<input type="text" id="ptype" name="ptype" autocomplete="off" class="layui-input">
							</div>
						</div>
						<div class="layui-inline">
							<label class="layui-form-label">产品收货地</label>
							<div class="layui-input-inline">
								<input type="text" id="destination" name="destination" autocomplete="off" class="layui-input">
							</div>
						</div>
						<div class="layui-inline">
							<button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
							<button type="reset" id="resetbtn" class="layui-btn layui-btn-primary"><i class="layui-icon"></i> 清 空</button>
						</div>
					</div>
				</form>
			</div>
		</fieldset>
        
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="show">二维码</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var table = layui.table;	

		var uid = $('#uid',parent.document).text();
			
		// 监听搜索操作
		form.on('submit(data-search-btn)', function (data) {
			var result = JSON.stringify(data.field);
			var pid= $("#pid").val();
			var ptype=$("#ptype").val();
			var destination=$("#destination").val();
			if(pid=="" || pid==null){
				pid=0;
			}
			if(ptype==null){
				ptype="";
			}
			if(destination==null){
				destination="";
			}
			if(pid==0 && ptype=="" && destination==""){
				layer.alert("请输入条件！");	
			}else{			
				var url = "http://localhost:9906/product/getProductByCondition?uid="+uid+"&pid="+pid+"&ptype="+ptype+"&destination="+destination;
				//执行搜索重载
				// tableLoad(url);
				$.ajax({
					type: "GET",
					url: url,
					dataType:"json",
					success:function(resultdata){						
						var option = {
						    elem: '#currentTableId',						
						    toolbar: '#toolbarDemo',
						    defaultToolbar: ['filter', 'exports', 'print', {
						        title: '提示',
						        layEvent: 'LAYTABLE_TIPS',
						        icon: 'layui-icon-tips'
						    }],
						    cols: [[
						        {field: 'pid', width: 150, title: '产品ID', sort: true},
						        {field: 'ptype', width: 100, title: '类别'},
						        {field: 'production', width: 100, title: '产地'},
						        {field: 'destination', width: 100, title: '目的地'},
						        {field: 'shipping', width: 140, title: '发货时间'},
						        {field: 'receiving', width: 140, title: '收货时间'},
						        {field: 'picking', width: 140, title: '采摘'},
						        {field: 'validity', width: 80, title: '保质期'},
						        {field: 'price', width: 80, title: '售价'},
						        {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
						    ]],
						    limits: [10, 15, 20, 25, 50, 100],
						    limit: 10,
						    page: true,
						    skin: 'line'
						};
						option.data = resultdata.data;
						if(option.data!=null){
							table.render(option);						
						}else{
							layer.alert("未查到数据");
						}
					}			
				});
			}

			return false;
		});
	
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑商品',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'http://localhost:9906/product/edit.html?pid='+data.pid,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {					
					$.ajax({
						url: 'http://localhost:9906/product/del',
						type: 'post',
						data: JSON.stringify(data),
						dataType: "json",
						contentType: "application/json",
						success:function(response){
							console.log(response);
							obj.del();	
							layer.close(index);
							layer.alert("删除成功");
						},
						error:function(){
							console.log("失败");
							layer.alert("删除失败");
						}					
					});
                });
            } else if (obj.event === 'show') {
				var url = "http://localhost:9906/tempImages/"+data.pid+".jpg";
				//iframe层			
				layer.open({
				  type: 2,
				  title: "二维码详情 ",
				  shadeClose: true,
				  shade: 0.2,
				  area: ['400px', '52%'],
				  content: url //iframe的url
				});        		
			}
        });
   		
		$("#resetbtn").click(function(){
			location.reload();					
		});				
	});
</script>

</body>
</html>